﻿using Microsoft.Data.Edm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.OData.Builder;
using WooCoo.Tools;

namespace WooCoo.DataPortal.WebHost.ODataServer
{
    internal static class HttpActionDescriptorExtensions
    {
        internal const string EdmModelKey = "MS_EdmModel";
        internal static IEdmModel GetEdmModel(this HttpActionDescriptor actionDescriptor, Type entityClrType)
        {
            if (actionDescriptor == null)
            {
                throw Error.ArgumentNull("actionDescriptor");
            }
            if (entityClrType == null)
            {
                throw Error.ArgumentNull("entityClrType");
            }
            return actionDescriptor.Properties.GetOrAdd("MS_EdmModel" + entityClrType.FullName, delegate(object _)
            {
                ODataConventionModelBuilder oDataConventionModelBuilder = new ODataConventionModelBuilder(actionDescriptor.Configuration, true);
                EntityTypeConfiguration entityType = oDataConventionModelBuilder.AddEntity(entityClrType);
                oDataConventionModelBuilder.AddEntitySet(entityClrType.Name, entityType);
                return oDataConventionModelBuilder.GetEdmModel();
            }) as IEdmModel;
        }
    }
}
